#!/bin/bash

help()
{
    cat <<- EOF
    Desc: 使用此脚本将生成符合NULSTAR规范的可执行子模块，
    	  所有子模块按照module.ncf配置，使用mvn命令进行打包，并生成启动、停止脚本
    Usage: ./package.sh
    		-b <branch> 打包前同步最新代码 参数为同步的远程分支名称
    		-p 打包前同步最新代码 从master分支拉取
    		-o <目录>  指定输出目录
    		-h 查看帮助
    		-j JAVA_HOME
    		-J 输出的jvm虚拟机目录，脚本将会把这个目录复制到程序依赖中
    		-i 跳过mvn打包
    		-z 生成压缩包
    		-N d打包时加入Nulstar模块
    		-a 添加一个打包模块
    		-r 移除一个打包模块
    		-s 查看配置的待打包模块列表
    Author: zlj
EOF
    exit 0
}

#日志打印函数
echoRed() { echo $'\e[0;31m'$1$'\e[0m'; } #print red
echoGreen() { echo $'\e[0;32m'$1$'\e[0m'; } #print green
echoYellow() { echo $'\e[0;33m'$1$'\e[0m'; } #print yellow
log(){ #print date prefix and green
    now=`date "+%Y-%m-%d %H:%M:%S"`
    echoGreen "$now $@"
}

BUILD_NULSTAR="1"
NULSTAR_FILE_NAME="nulstar_190909.tar.gz"
#TARGET_OS=linux
#NULSTAR download url
NULSTAR_URL="http://nuls-cn.oss-cn-hangzhou.aliyuncs.com/nulstar/"
#获取参数
#输出目录
NULS_WALLET_TAR_NAME="./NULS_Wallet"
MODULES_PATH="${NULS_WALLET_TAR_NAME}"
#RELEASE_OUT_PATH="./NULS-Walltet-linux64-alpha1"
#是否马上更新代码
DOPULL=
#是否生成mykernel模块
DOMOCK=
#更新代码的 git 分支
GIT_BRANCH=

#项目根目录
cd `dirname $0`
PROJECT_PATH=`pwd`;
cd $PROJECT_PATH;
#打包工作目录
BUILD_PATH="${PROJECT_PATH}/build";
#打包配置文件
PACKAGE_CONFIG="`pwd`/.package.ncf"
if [ ! -f "$PACKAGE_CONFIG" ]; then
    cp "${BUILD_PATH}/package-base.ncf" $PACKAGE_CONFIG
fi
NULS_NCF=${PROJECT_PATH}/config/nuls.ncf
GENESIS_BLOCK=${PROJECT_PATH}/config/genesis-block.json
#编辑.package.ncf
updatePackage(){
    c="sed -n '/^$1/p' $PACKAGE_CONFIG"
    has=`eval $c`;
    if [ -n "$has" ];
    then
        os=`uname`
        if [ "$os" == "Darwin" ];
        then
            eval "sed -i '' 's/^$has/$1=$2/' $PACKAGE_CONFIG"
        else
            eval "sed -i 's/^$has/$1=$2/' $PACKAGE_CONFIG"
        fi
    else
        if [ $2 == "1" ]; then
            echo "${1}=1" >> $PACKAGE_CONFIG
            echo "" >> $PACKAGE_CONFIG
        fi
    fi
    exit 0
}

showPackageModule(){
    log "=========================="
    log "package module list"
    while read line
    do
        TEMP=`echo $line|grep -Eo '\[.+\]'`
        if [ -z "$TEMP" ]; then
            isPackage=`echo $line |grep "=1"`
            if [ -n "$isPackage" ]; then
                log "---->$line" | awk -F '=' '{print $1}'
            fi
        fi
    done < $PACKAGE_CONFIG
    log "=========================="
}

setTargetOS(){
    case "$1" in
		linux64)
		     TARGET_OS=linux;;
		linux)
		     TARGET_OS=linux;;
		macos)
		     TARGET_OS=macos;;
		win64)
		     TARGET_OS=window;;
		window)
		     TARGET_OS=window;;
		?)   exit 2;;
	esac
	NULSTAR_FILE_NAME=${TARGET_OS}_${NULSTAR_FILE_NAME}
}

while getopts phb:o:j:iJ:zNa:r:sO:B name
do
            case $name in
            p)	   DOPULL=1
            	   GIT_BRANCH="master";;
            b)     DOPULL=1
				   GIT_BRANCH="$OPTARG"
					;;
			o)	   MODULES_PATH="$OPTARG";;
			O)     setTargetOS "$OPTARG";;
			h)     help ;;
			j)     JAVA_HOME="$OPTARG";;
			i)     IGNROEMVN="1";;
			J)     JRE_HOME="$OPTARG";;
			z)     BUILDTAR="1";;
			N)     BUILD_NULSTAR="1";;
			a)     updatePackage "$OPTARG" 1;;
			r)     updatePackage "$OPTARG" 0;;
			s)     showPackageModule
			       exit 0
			       ;;
			B)     NULS_NCF=${PROJECT_PATH}/config/nuls.beta.ncf
                   GENESIS_BLOCK=${PROJECT_PATH}/config/genesis-block.beta.json
                   ;;
            ?)     exit 2;;
           esac
done

# default packing nulstar for linux platform
if [ -z "$TARGET_OS" ]; then
    setTargetOS "linux"
fi

# 检查java版本 must be 11
checkJavaVersion(){
    JAVA="$JAVA_HOME/bin/java"
    if [ ! -r "$JAVA" ]; then
        JAVA='java'
    fi

    JAVA_EXIST=`${JAVA} -version 2>&1 |grep 11`
    if [ ! -n "$JAVA_EXIST" ]; then
            log "JDK version is not 11"
            ${JAVA} -version
            exit 0
    fi
}

checkJavaVersion

#执行mvn函数打包java工程  $1 命令 $2 模块名称
doMvn(){
    if [ -n "$IGNROEMVN" ]; then
        log "skip mvn package";
        return ;
    fi
	log "mvn $1 $2"
	moduleLogDir="${BUILD_PATH}/tmp/$2";
	if [ ! -d ${moduleLogDir} ]; then
		mkdir ${moduleLogDir}
	fi
	installLog="${moduleLogDir}/log.log";
	mvn clean $1 -Dmaven.test.skip=true > "${installLog}" 2>&1
	mvnSuccess=`grep "BUILD SUCCESS" ${installLog}`
	if [ ! -n "$mvnSuccess" ]; then
		echoRed "$1 $2 FAIL"
		echoRed "日志文件:${installLog}"
		cd ..
		exit 0
	fi
	# rm $installLog;
	log "$1 $2 success"
}


log "working path is $PROJECT_PATH";

log "loading package config : ${PACKAGE_CONFIG}"
#读取要打包的模块信息
showPackageModule
if [ ! -d "${BUILD_PATH}/tmp" ]; then
	mkdir "${BUILD_PATH}/tmp"
fi

if [ ! -d "${MODULES_PATH}" ]; then
	mkdir "${MODULES_PATH}"
fi
MODULES_PATH=`cd "$MODULES_PATH"; pwd`
RELEASE_PATH=$MODULES_PATH
echoYellow "Modules Path $MODULES_PATH"''
log "==================BEGIN PACKAGE MODULES=============================="
declare -a managedModules
#if [ ! -d "$MODULES_PATH/bin" ]; then
#	mkdir $MODULES_PATH/bin
#fi
#存放脚本目录
MODULES_BIN_PATH=$MODULES_PATH
if [ ! -d "$MODULES_PATH/Modules" ]; then
	#statements
	mkdir $MODULES_PATH/Modules
fi
#模块数据库文件存放位置
COMMON_DATA_PATH=$MODULES_PATH/data
if [ ! -d ${COMMON_DATA_PATH} ]; then
    mkdir ${COMMON_DATA_PATH}
fi

#默认日志目录
MODULES_LOGS_PATH=${MODULES_PATH}/Logs
if [ ! -d "$MODULES_LOGS_PATH" ]; then
	#statements
	mkdir $MODULES_LOGS_PATH
fi
MODULES_PATH=$MODULES_PATH/Modules
#创建NULS_2.0公共模块目录
if [ ! -d "$MODULES_PATH/Nuls" ]; then
	mkdir $MODULES_PATH/Nuls
fi
MODULES_PATH=$MODULES_PATH/Nuls
#模块公共依赖jar存放目录
COMMON_LIBS_PATH=$MODULES_PATH/libs
if [ -z "${IGNROEMVN}" ]; then
    if [ -d ${COMMON_LIBS_PATH} ]; then
        rm -r ${COMMON_LIBS_PATH}
    fi
    mkdir ${COMMON_LIBS_PATH}
fi


#0.更新代码
if [ -n "${DOPULL}" ];then
	log "git pull origin $GIT_BRANCH"
	git pull origin "$GIT_BRANCH"
fi
echo "`cat version`-`git rev-parse --short HEAD`" > .package-version
PACKAGE_VERSION=`cat .package-version`

#0.download Nulstar
if [ -n  "${BUILD_NULSTAR}" ]; then
    log "build Nulstar "
    log "target os : $TARGET_OS"

    if [ ! -f "${BUILD_PATH}/tmp/${NULSTAR_FILE_NAME}" ];
     then
        log "download Nulstar"
        wget $NULSTAR_URL${NULSTAR_FILE_NAME}
        mv "./${NULSTAR_FILE_NAME}" ${BUILD_PATH}/tmp/${NULSTAR_FILE_NAME}
    else
        log "found ${BUILD_PATH}/tmp/${NULSTAR_FILE_NAME}"
    fi
    if [ -f "${BUILD_PATH}/tmp/${NULSTAR_FILE_NAME}" ]; then
        tar -xvf "${BUILD_PATH}/tmp/${NULSTAR_FILE_NAME}" -C "${BUILD_PATH}/tmp"
        cp -Rf "${BUILD_PATH}/tmp/Nulstar/Modules" ${RELEASE_PATH}
        cp -Rf "${BUILD_PATH}/tmp/Nulstar/Libraries" ${RELEASE_PATH}
        if [ "${TARGET_OS}" == "window" ];
        then
            cp "${BUILD_PATH}/nulstar/start.bat" "${RELEASE_PATH}/start.bat"
            cp "${BUILD_PATH}/nulstar/stop.bat"  "${RELEASE_PATH}/stop.bat"
        else
            cp "${BUILD_PATH}/nulstar/start.sh" "$RELEASE_PATH/start"
            cp "${BUILD_PATH}/nulstar/stop.sh" "$RELEASE_PATH/stop"
            chmod +x "${RELEASE_PATH}/start"
            chmod +x "${RELEASE_PATH}/stop"
        fi

    fi
    log "build Nulstar done"
fi

rm -rf ${COMMON_LIBS_PATH}/*
doMvn "clean package" "nuls-project"

#检查module.ncf指定配置项是否存在
checkModuleItem(){
	if [ ! -f "./module.ncf" ]; then
		return 0
	fi
	if [ -z "$1" ]; then
		echoRed "getModuleItem 必须传入配置项名称"
		exit 1
	fi
    while read line
	do
		pname=`echo $line | awk -F '=' '{print $1}'`
		if [ "${pname}" == "$1" ]; then
			return 1;
		fi
	done < "$(pwd)/module.ncf"
	echoRed "$2 module.ncf 必须配置 $1"
	exit 0
}

getModuleItem(){
    moduleFile="./module.ncf"
    if [ -n "$2" ]; then
       moduleFile=$2
    fi
    while read line
	do
		pname=`echo $line | awk -F '=' '{print $1}'`
		pvalue=`awk -v a="$line" '
						BEGIN{
							len = split(a,ary,"=")
							r=""
							for ( i = 2; i <= len; i++ ){
								if(r != ""){
									r = (r"=")
								}
								r=(r""ary[i])
					 		}
							print r
						}
					'`
		if [ "${pname}" == $1 ]; then
			echo ${pvalue};
			return 1;
		fi
	done < $moduleFile
	return 0
}


hasPackageModule(){
    echo `getModuleItem $1 ${PACKAGE_CONFIG}`
}


#拷贝打好的jar包到Moules/Nuls/<Module Name>/<Version> 下
copyJarToModules(){
#    if [ -z "$IGNROEMVN" ]; then
#       doMvn "clean package" $1
#    fi
	moduleName=`getModuleItem "APP_NAME"`;
	version=`getModuleItem "VERSION"`;
	privateDependent=`getModuleItem "PrivateDependent"`;
	if [ ! -d "${MODULES_PATH}/${moduleName}" ];then
		mkdir ${MODULES_PATH}/${moduleName}
	fi
	if [ -d "${MODULES_PATH}/${moduleName}/${version}" ]; then
		rm -r "${MODULES_PATH}/${moduleName}/${version}"
	fi
	moduleOutPath="${MODULES_PATH}/${moduleName}/${version}"
	if [ ! -d "$moduleOutPath" ]; then
	    mkdir $moduleOutPath
	fi
	modulePriLibPath="${moduleOutPath}/lib"
	if [ ! -d "${modulePriLibPath}" ]; then
	    mkdir $modulePriLibPath
	    else
	    rm -rf "${modulePriLibPath}/*"
	fi
	jarName=`ls target |grep .jar`
	nowPath=`pwd`
	echo "copy ${nowPath}/target/${moduleName}-${version}.jar to ${MODULES_PATH}/${moduleName}/${version}/${moduleName}-${version}.jar"
	cp ./target/${jarName} "${MODULES_PATH}/${moduleName}/${version}/${moduleName}-${version}.jar"
	dependentListConf=./target/dependentList
	if [ -f "$dependentListConf" ]; then
	    rm -f $dependentListConf
	fi
	touch $dependentListConf;
	if [ -d ./target/libs ]; then
		for jar in `ls ./target/libs`; do
		    echo "$jar" >> $dependentListConf
#		    isPriDependent=`awk -v a="$privateDependent" -v j="$jar" '
#						BEGIN{
#							len = split(a,ary,",")
#							for ( i = 1; i <= len; i++ ){
#							    if(index(j,ary[i])){
#							        print j
#							    }
#					 		}
#						}
#					'`
#			if [ -n "$isPriDependent" ]; then
#			   cp "./target/libs/${jar}" "${modulePriLibPath}"
#			   else
#			   #statement
               if [ ! -f ${COMMON_LIBS_PATH}/${jar} ]; then
			       cp "./target/libs/${jar}" "${COMMON_LIBS_PATH}"
			   fi
#			fi
		done
		cp "$dependentListConf" "${MODULES_PATH}/${moduleName}/${version}/dependent.conf"
	fi
	loadLanguage=`getModuleItem "loadLanguage"`;
	if [ "$loadLanguage" == "1" ]; then
	    cp -r "${BUILD_PATH}/gen_languages" "${moduleOutPath}/languages"
	fi

}


copyModuleNcfToModules(){
	moduleName=`getModuleItem "APP_NAME"`;
	version=`getModuleItem "VERSION"`;
	mainClass=`getModuleItem "MAIN_CLASS"`;
	mainClassName=`awk -v s="${mainClass}" 'BEGIN{ len = split(s,ary,"."); print ary[len]}'`
	moduleDynamicDependent=`getModuleItem "moduleDynamicDependent"`;  #模块动态依赖
	moduleBuildPath="${BUILD_PATH}/tmp/$1"
	if [ ! -d "${moduleBuildPath}" ]; then
		mkdir "${moduleBuildPath}"
	fi
	moduleNcf="${moduleBuildPath}/module.1.ncf";
	if [ -f $moduleNcf ]; then
		rm $moduleNcf
	fi
	touch $moduleNcf
	cfgDomain=""
	sedCommand="sed "
	while read line
	do
		TEMP=`echo $line|grep -Eo '\[.+\]'`
		if [ -n "$TEMP" ]; then
#		  echo "set cfg domain ${TEMP}"
		  cfgDomain=$TEMP
		fi
		if [ "${cfgDomain}" == "[JAVA]" -a ! -n "$TEMP" ];
		then
			pname=`echo $line | awk -F '=' '{print $1}'`
			#pvalue=$(echo $line | awk -F '=' '{print $2}')
			pvalue=`awk -v a="$line" '
						BEGIN{
							len = split(a,ary,"=")
							r=""
							for ( i = 2; i <= len; i++ ){
								if(r != ""){
									r = (r"=")
								}
								r=(r""ary[i])
					 		}
							print r
						}
					'`
            if [ "${pname}" != "" ]; then
			    sedCommand+=" -e 's/%${pname}%/${pvalue}/g' "
			fi
			echo $line >> $moduleNcf
		else

			if [ "${cfgDomain}" != "[JAVA]" ]; then
				echo $line >> $moduleNcf
			fi
		fi
	done < ./module.ncf
	#给模块添加动态依赖
	dependent=
    if [ -n "${moduleDynamicDependent}" ]; then
        IFS=',' read -ra ddm <<< "${moduleDynamicDependent}"
        for i in "${ddm[@]}"; do
            isPackage=`hasPackageModule $i`
            if [ "${isPackage}" == "1" ]; then
                if [ -n "$dependent" ]; then
                    dependent="${dependent},"
                fi
                dependent="${dependent}${i}"
            fi
        done

        if [ -n "$dependent" ];
        then
            log "dynamic dependent : $dependent"
            echo "dependent=$dependent" >> $moduleNcf
        fi
    fi
#	 merge common module.ncf and private module.ncf to module.tmep.ncf
	"${PROJECT_PATH}/build/merge-ncf" "${BUILD_PATH}/module-prod.ncf" $moduleNcf $moduleName
#	rm $moduleNcf
	sedCommand+=" -e 's/%MAIN_CLASS_NAME%/${mainClassName}/g' "
#    echo $sedCommand
	if [ -z `echo "${sedCommand}" | grep -o "%JOPT_XMS%"` ]; then
		sedCommand="${sedCommand}  -e 's/%JOPT_XMS%/256/g' "
	fi
	if [ -z `echo "${sedCommand}" | grep -o "%JAVA_OPTS%"` ]; then
		sedCommand="${sedCommand}  -e 's/%JAVA_OPTS%//g' "
	fi
	if [ -z `echo "${sedCommand}" | grep -o "%JOPT_XMX%"` ]; then
		sedCommand="${sedCommand}  -e 's/%JOPT_XMX%/256/g' "
	fi
	if [ -z `echo "${sedCommand}" | grep -o "%JOPT_METASPACESIZE%"` ]; then
		sedCommand="${sedCommand}  -e 's/%JOPT_METASPACESIZE%/128/g' "
	fi
	if [ -z `echo "${sedCommand}" | grep -o "%JOPT_MAXMETASPACESIZE%"` ]; then
		sedCommand="${sedCommand}  -e 's/%JOPT_MAXMETASPACESIZE%/256/g' "
	fi
    if [ -d "./script" ];
    then
        for file in `ls ./script`
        do
            nowPath=`pwd`
            eval "${sedCommand}  ${nowPath}/script/${file} > ${moduleBuildPath}/${file}"
            cp "${moduleBuildPath}/${file}" "${MODULES_PATH}/${moduleName}/${version}/${file}"
            chmod u+x "${MODULES_PATH}/${moduleName}/${version}/${file}"
            echo "copy ${moduleBuildPath}/${file} to ${MODULES_PATH}/${moduleName}/${version}/${file}"
        done
    else
    	startSh="${BUILD_PATH}/start-temp"
        startBat="${BUILD_PATH}/start-temp.bat"
        stopSh="${BUILD_PATH}/stop-temp"
        stopBat="${BUILD_PATH}/stop-temp.bat"
        # echo $sedCommand
        eval "${sedCommand}  ${startSh} > ${moduleBuildPath}/start.sh"
        cp "${moduleBuildPath}/start.sh" "${MODULES_PATH}/${moduleName}/${version}/start.sh"
        chmod +x "${MODULES_PATH}/${moduleName}/${version}/start.sh"
        echo "copy ${moduleBuildPath}/start.sh to ${MODULES_PATH}/${moduleName}/${version}/start.sh"

        eval "${sedCommand}  ${startBat} > ${moduleBuildPath}/start.bat"
        cp "${moduleBuildPath}/start.bat" "${MODULES_PATH}/${moduleName}/${version}/start.bat"
    #    cp "${moduleBuildPath}/start.bat" "/Volumes/share/start.bat"
        echo "copy ${moduleBuildPath}/start.bat to ${MODULES_PATH}/${moduleName}/${version}/start.bat"

        eval "${sedCommand}  ${stopSh} > ${moduleBuildPath}/stop.sh"
        cp "${moduleBuildPath}/stop.sh" "${MODULES_PATH}/${moduleName}/${version}/stop.sh"
        chmod +x "${MODULES_PATH}/${moduleName}/${version}/stop.sh"
        echo "copy ${moduleBuildPath}/stop.sh to ${MODULES_PATH}/${moduleName}/${version}/stop.sh"

        eval "${sedCommand}  ${stopBat} > ${moduleBuildPath}/stop.bat"
        cp "${moduleBuildPath}/stop.bat" "${MODULES_PATH}/${moduleName}/${version}/stop.bat"
        #cp "${moduleBuildPath}/stop.bat" "/Volumes/share/stop.bat"
        echo "copy ${moduleBuildPath}/stop.bat to ${MODULES_PATH}/${moduleName}/${version}/stop.bat"

    fi
	cp "${moduleBuildPath}/module.temp.ncf" "${MODULES_PATH}/${moduleName}/${version}/Module.ncf"
	echo "copy ${moduleBuildPath}/module.temp.ncf to ${MODULES_PATH}/${moduleName}/${version}/Module.ncf"
}

#2.遍历文件夹，检查第一个pom 发现pom文件后通过mvn进行打包，完成后把文件jar文件和module.ncf文件复制到Modules文件夹下
packageModule() {
	if [ ! -d "./$1" ]; then
		return 0
	fi
	if [ "$1" == "tmp" ]; then
	    return 0
	fi
	cd ./$1
#	echo `pwd`
#	echo ${RELEASE_PATH}
	if [ `pwd` == "${RELEASE_PATH}" ]; then
	    cd ..
		return 0;
	fi
	nowPath=`pwd`
	if [ -f "./module.ncf" ]; then
		echoYellow "find module.ncf in ${nowPath}"
		if [ ! -f "./pom.xml" ]; then
			echoRed "模块配置文件必须与pom.xml在同一个目录 : ${nowPath}"
			exit 0;
		fi
		moduleName=`getModuleItem "APP_NAME"`;
		package=`hasPackageModule "$moduleName"`;
		if [ "${package}" == "1" ];
		then
            checkModuleItem "APP_NAME" "$1"
            checkModuleItem "VERSION" "$1"
            checkModuleItem "MAIN_CLASS" "$1"
		    log "build $1"
            copyJarToModules $1
            copyModuleNcfToModules $1
            managed=`getModuleItem "Managed"`;
            if [ "${managed}" == "1" ]; then
                managedModules[${#managedModules[@]}]="$moduleName"
            fi
            log "build $moduleName done"
        else
            rm -rf "$MODULES_PATH/${moduleName}"
            echoYellow "$moduleName skip"
		fi
		cd ..
		return 0
	fi
    for f in `ls`
    do
        packageModule $f
    done
    cd ..
}

#log "INSTALL REQUIRE MODULE"
#for fi in `ls`
#do
#    installModule $fi
#done
log "PACKAGE MODULE"
for fi in `ls`
do
    packageModule $fi
done
log "============ PACKAGE MODULES DONE ==============="
cd $PROJECT_PATH

moduleListFile=${MODULES_BIN_PATH}/.modules
#echo $moduleListFile
if [ -f "${moduleListFile}" ] ; then
    rm -f $moduleListFile
fi
for module in ${managedModules[@]}
do
    echo "$module" >> $moduleListFile
done
if [ -n "${JRE_HOME}" ]; then
log "============ COPY JRE TO libs ==================="

    if [ ! -d "${JRE_HOME}" ];
    then
        echoRed "JRE_HOME 必须是文件夹"
        else
        log "JRE_HOME IS ${JRE_HOME}"
        LIBS_PATH="${RELEASE_PATH}/Libraries"
        if [ ! -d "${LIBS_PATH}" ]; then
            mkdir ${LIBS_PATH}
        fi
        if [ ! -d "${LIBS_PATH}/JAVA" ]; then
            mkdir "${LIBS_PATH}/JAVA"
        fi
        if [ ! -d "${LIBS_PATH}/JAVA/JRE" ]; then
            mkdir "${LIBS_PATH}/JAVA/JRE"
        fi
        rm -Rf "${LIBS_PATH}/JAVA/JRE/11.0.2"
        cp -r ${JRE_HOME} "${LIBS_PATH}/JAVA/JRE/11.0.2"
    fi
log "============ COPY JRE TO libs done ============"
fi

log "================ COPY SCRIPT ==============="
	eval "sed -e 's/%PACKAGE_VERSION%/${PACKAGE_VERSION}/g' ${NULS_NCF} > ${MODULES_BIN_PATH}/nuls.ncf"
	cp $GENESIS_BLOCK ${MODULES_BIN_PATH}/genesis-block.json
	cp "${BUILD_PATH}/cmd" "${MODULES_BIN_PATH}/"
	cp "${BUILD_PATH}/cmd.bat" "${MODULES_BIN_PATH}/"
	chmod u+x "${MODULES_BIN_PATH}/cmd"
	cp "${BUILD_PATH}/create-address" "${MODULES_BIN_PATH}/"
	chmod u+x "${MODULES_BIN_PATH}/create-address"
	cp "${BUILD_PATH}/test" "${MODULES_BIN_PATH}/"
	chmod u+x "${MODULES_BIN_PATH}/test"
	cp "${BUILD_PATH}/func" "${MODULES_BIN_PATH}/"
	chmod u+x "${MODULES_BIN_PATH}/func"
	tempModuleList=
	for m in ${managedModules[@]}
	do
	    tempModuleList+=" \"${m}\""
	done
	eval "sed -e 's/%MODULES%/${tempModuleList}/g' ${BUILD_PATH}/check-status > ${BUILD_PATH}/tmp/check-status-temp"
	cp "${BUILD_PATH}/tmp/check-status-temp" "${MODULES_BIN_PATH}/check-status"
	chmod u+x "${MODULES_BIN_PATH}/check-status"
	cp ${PROJECT_PATH}/version ${MODULES_BIN_PATH}/version
log "===============  COPY SCRIPT DONE ==========="
DOMOCK=`hasPackageModule "mykernel"`
if [ -n "${DOMOCK}" ]; then
	log "============== BUILD start-dev script ====================="
	cp "${BUILD_PATH}/start-mykernel" "${MODULES_BIN_PATH}/start-dev"
	chmod u+x "${MODULES_BIN_PATH}/start-dev"
	cp "${BUILD_PATH}/stop-mykernel" "${MODULES_BIN_PATH}/stop-dev"
	chmod u+x "${MODULES_BIN_PATH}/stop-dev"
	log "============== BUILD start-dev script done ================"
fi

#临时删除window版本未实现的脚本
if [ "$TARGET_OS" == "window" ]; then
    rm -f "${MODULES_BIN_PATH}/start-dev"
    rm -f "${MODULES_BIN_PATH}/stop-dev"
    rm -f "${MODULES_BIN_PATH}/check-status"
    rm -f "${MODULES_BIN_PATH}/test"
    rm -f "${MODULES_BIN_PATH}/create-address"
fi

if [ -n "${BUILDTAR}" ]; then
    log "============ BUILD ${RELEASE_PATH}.tar.gz ==================="
    tar -zcPf "${NULS_WALLET_TAR_NAME}_${TARGET_OS}.tar.gz" ${NULS_WALLET_TAR_NAME}
    log "============ BUILD ${NULS_WALLET_TAR_NAME}_${TARGET_OS}.tar.gz FINISH==================="
fi
cd $PROJECT_PATH
echo "package version : $PACKAGE_VERSION"
log "============ ${RELEASE_PATH} PACKAGE FINISH 🍺🍺🍺🎉🎉🎉 ==============="